﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntLibEC.Biz;
using EntLibEC.Cmn;
using EntLibEC.Biz.Online;
using EntLibEC.Info.Online;
using System.Data;
using System.Collections;

namespace EntLibEC.View.Online
{
    public partial class OnlineListAreaOpt : PageBase
    {
        private string closeAndRefresh = "javascript:window.opener.__doPostBack('btnSearch','btnSearch');window.close(); return false;";

        protected void Page_Load(object sender, EventArgs e)
        {
            btnClose.Attributes["onclick"] = closeAndRefresh;

            if (!Page.IsPostBack)
            {
                ucCategory.BindC1(true);
                ucPagePosition.BindPagePosition(true);

                int sysno = 0;
                OnlineListAreaInfo onlineListAreaInfo = null;

                if (Request.QueryString["sysno"] != null)
                {
                    try
                    {
                        sysno = Convert.ToInt32(Request.QueryString["sysno"].ToString());
                        onlineListAreaInfo = OnlineListAreaManager.GetInstance().Load(sysno);
                    }
                    catch
                    {
                        Assert(lblMessage, "传入的sysno 参数有误。", -1);
                        return;
                    }

                    if (onlineListAreaInfo != null)
                    {
                        bindForm(onlineListAreaInfo);
                    }
                }
            }

            string opt = string.Empty;

            btnAdd.Visible = false;
            btnUpdate.Visible = false;
            btnDelete.Visible = false;

            if (Request.QueryString["opt"] != null)
            {
                opt = Request.QueryString["opt"].Trim().ToLower();
                if (opt == "new")
                    btnAdd.Visible = true;
                else if (opt == "update")
                    btnUpdate.Visible = true;
                else if (opt == "delete")
                {
                    btnDelete.Visible = true;
                    txtListAreaName.ReadOnly = true;
                    txtListAreaKey.ReadOnly = true;
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!base.CheckAccess((int)AppEnum.Privilege.OnlineList, false, lblMessage))
                return;

            if (!checkForm())
                return;

            try
            {
                OnlineListAreaInfo onlineListAreaInfo = new OnlineListAreaInfo();
                onlineListAreaInfo.ListAreaName = txtListAreaName.Text.Trim();
                onlineListAreaInfo.ListAreaKey = Convert.ToInt32(txtListAreaKey.Text.Trim());
                onlineListAreaInfo.Category1SysNo = ucCategory.C1SysNo;
                onlineListAreaInfo.PagePosition = ucPagePosition.PagePositionSysNo;
                onlineListAreaInfo.ListOrder = txtListOrder.Text.Trim();
                onlineListAreaInfo.CreateUserSysNo = base.sInfo.User.SysNo;
                onlineListAreaInfo.CreateTime = DateTime.Now;

                OnlineListAreaManager.GetInstance().Insert(onlineListAreaInfo);

                txtSysNo.Text = onlineListAreaInfo.SysNo.ToString();

                Assert(lblMessage, "新增成功。", 1);
            }
            catch (BizException exp)
            {
                Assert(lblMessage, exp.Message, -1);
            }
            catch (Exception exp)
            {
                Assert(lblMessage, "操作失败，请检查日志。", -1);
                ErrorLog.GetInstance().Write(exp.ToString());
            }
        }

        private bool checkForm()
        {
            if (txtListAreaName.Text.Trim().Length == 0)
            {
                Assert(lblMessage, "请输入显示区域名称。", -1);
                return false;
            }
            if (txtListAreaKey.Text.Trim().Length == 0)
            {
                Assert(lblMessage, "请输入显示区域主键。", -1);
                return false;
            }
            if (!Util.IsNaturalNumber(txtListAreaKey.Text.Trim()))
            {
                Assert(lblMessage, "显示区域主键必须为正整数。", -1);
                return false;
            }
            return true;
        }

        private void bindForm(OnlineListAreaInfo onlineListAreaInfo)
        {
            txtSysNo.Text = onlineListAreaInfo.SysNo.ToString();
            txtListAreaName.Text = onlineListAreaInfo.ListAreaName;
            txtListAreaKey.Text = onlineListAreaInfo.ListAreaKey.ToString();
            ucCategory.C1SysNo = onlineListAreaInfo.Category1SysNo;
            ucPagePosition.PagePositionSysNo = onlineListAreaInfo.PagePosition;
            txtListOrder.Text = onlineListAreaInfo.ListOrder;

            Hashtable ht = new Hashtable();
            ht.Add("ListArea", onlineListAreaInfo.ListAreaKey);
            DataSet ds = OnlineListManager.GetInstance().GetOnlineListDs(ht, AppConst.IntNull, false, false);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!base.CheckAccess((int)AppEnum.Privilege.OnlineList, false, lblMessage))
                return;

            if (!checkForm())
                return;

            try
            {
                OnlineListAreaInfo onlineListAreaInfo = new OnlineListAreaInfo();
                onlineListAreaInfo.SysNo = Convert.ToInt32(txtSysNo.Text);
                onlineListAreaInfo.ListAreaName = txtListAreaName.Text.Trim();
                onlineListAreaInfo.ListAreaKey = Convert.ToInt32(txtListAreaKey.Text.Trim());
                onlineListAreaInfo.Category1SysNo = ucCategory.C1SysNo;
                onlineListAreaInfo.PagePosition = ucPagePosition.PagePositionSysNo;
                onlineListAreaInfo.ListOrder = txtListOrder.Text.Trim();
                onlineListAreaInfo.CreateUserSysNo = base.sInfo.User.SysNo;
                onlineListAreaInfo.CreateTime = DateTime.Now;

                OnlineListAreaManager.GetInstance().Update(onlineListAreaInfo);

                txtSysNo.Text = onlineListAreaInfo.SysNo.ToString();

                Assert(lblMessage, "更新成功。", 1);
            }
            catch (BizException exp)
            {
                Assert(lblMessage, exp.Message, -1);
            }
            catch (Exception exp)
            {
                Assert(lblMessage, "操作失败，请检查日志。", -1);
                ErrorLog.GetInstance().Write(exp.ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!base.CheckAccess((int)AppEnum.Privilege.OnlineList, false, lblMessage))
                return;

            if (txtSysNo.Text.Trim().Length == 0 || txtListAreaKey.Text.Trim().Length == 0)
            {
                Assert(lblMessage, "系统编号或显示区域主键为空。", -1);
                return;
            }
            else
            {
                try
                {
                    int sysno = Convert.ToInt32(txtSysNo.Text);
                    int listAreaKey = Convert.ToInt32(txtListAreaKey.Text);

                    OnlineListAreaManager.GetInstance().Delete(sysno, listAreaKey);

                    Assert(lblMessage, "删除成功。", 1);
                }
                catch (BizException ex)
                {
                    Assert(lblMessage, ex.Message, -1);
                }
                catch (Exception ex)
                {
                    Assert(lblMessage, "操作失败，请检查日志。", -1);
                    ErrorLog.GetInstance().Write(ex.ToString());
                }
            }
        }
    }
}
